/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.renderer;

import com.jb.framework.datasource.TableModel;
import com.jb.web.bean.FormField;
import com.jb.web.bean.ListBox;
import com.jb.web.context.WebContext;

import java.util.Iterator;

import com.jb.faces.component.UIComponent;

import java.util.ArrayList;
import java.util.Collection;


public class ListBoxRenderer extends FormItemRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent itemToRender) {
        FormField formField = (FormField) itemToRender;
        StringBuffer html = new StringBuffer("");
        String value = evaluate(pCtx, formField, formField.getValue());

        if (isVisible(pCtx, formField)) {
            html.append(optionField(pCtx, formField));
        } else {
            html.append("<input type =\"hidden\" name=\"" + formField.getClientId() + "\"")
                .append(addTagProperty(" value", value, null)).append(">");
        }
        return html;
    }

    private String optionField(WebContext pCtx, FormField formItem) {
        ListBox select = (ListBox) formItem;
        StringBuffer html = new StringBuffer("<select name=\"" + select.getFieldName() + "\"");
        if (!isEditable(pCtx, select)) {
            html.append(addTagProperty(" onchange", select.getOnChange(), null));
        }

        html.append(getEditScript(pCtx, select));
        html.append(addTagProperty(" multiple", "multiple", null));

        if (pCtx.isRunTime()) {
            html.append(addTagProperty(" disabled", select.isEnabled() ? "" : "disabled", null));
        }

        html.append(addTagProperty(" style", select.getStyle(pCtx), null)).append(">\n");

        TableModel tableData = optionData(pCtx, select, select.getValueOptions());
        Collection list =(Collection) select.getModelBean();
        if(list==null)
            list=new ArrayList();
        if (tableData != null) {
            for (int i = 0; i < tableData.getMaxValue(); i++) {
                try {
                    Iterator row = tableData.getItem(i);
                    String dbName =  row.next().toString();
                    String dbValue = row.next().toString();

                    if (list.contains(dbValue)) {
                        html.append("<option value=\"" + dbValue + "\" selected>" + dbName + "</option>\n");
                    } else {
                        html.append("<option value=\"" + dbValue + "\">" + dbName + "</option>\n");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        html.append("</select>");
        return html.toString();
    }
}
